﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.NoticeManage
{
    public partial class NoticeEdit : SecurityPage
    {
        public override string PrivCode()
        {
            return "d43afe127ee13af5";
        }

        private void DropBanbing()
        {
            List<xy_bmxbm> xylist = xy_bmxbm.Where("BMFQID=@0", "03").ToList();
            xylist.Add(new xy_bmxbm() { BMDM = "0", BMMC = "全校" });
            this.Drop_xy.DataSource = xylist.OrderBy(p => p.BMDM).ToList();
            this.Drop_xy.DataValueField = "BMDM";
            this.Drop_xy.DataTextField = "BMMC";
            this.Drop_xy.DataBind();
        }

        private void LoadData()
        {
            string EditId = Request.QueryString["EditId"].ToString();
            Notice md = Notice.Where("id=@0", int.Parse(EditId)).First();
            this.txt_title.Text = md.Title;
            this.FCKeditor1.Value = md.Contents;
            this.Drop_xy.SelectedValue = md.XYDM;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            this.DropBanbing();
            if (Request.QueryString["EditId"] != null)
                this.LoadData();
        }

        protected void Btn_Save_Click(object sender, EventArgs e)
        {
            Notice md;
            if (Request.QueryString["EditId"] == null)
            {
                md = new Notice();
            }
            else
            {
                md = Notice.Where("id=@0", int.Parse(Request.QueryString["EditId"].ToString())).First();
            }

            md.Userid =0;//??? 应该为登录人的
            md.Title = this.txt_title.Text.Trim();
            md.Contents = this.FCKeditor1.Value;
            md.AddTime = DateTime.Now;
            md.XYDM = this.Drop_xy.SelectedValue;
            if (Request.QueryString["EditId"] == null)
            {
                Data.News.Insert(md);
            }
            else
            {
                Data.News.Update(md, "id=@0", int.Parse(Request.QueryString["EditId"].ToString()));
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');window.location.href='NoticeList.aspx';</script>");
        }

        protected void Btn_Return_Click(object sender, EventArgs e)
        {
            Response.Redirect("NoticeList.aspx");
        }
    }
}